{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21cfea06-af98-496a-b13b-106c335a2e65",
   "metadata": {},
   "source": [
    "# Understand Number Space - MinMax Mode\n",
    "\n",
    "To enable searches with well known MAX and MIN logic `NumberSpace` creates the opportunity to embed numeric data in a searchable way.\n",
    "\n",
    "Check Similar mode [here](number_embedding_similar.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bf67328-efe5-4c88-9c36-ce2d4b20d89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==37.5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11664035-fff3-4c38-97f3-f2fbb0d46778",
   "metadata": {},
   "outputs": [],
   "source": [
    "from superlinked import framework as sl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5dbad3ef-0e66-4e02-920f-1041eca72787",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person(sl.Schema):\n",
    "    id: sl.IdField\n",
    "    height: sl.Float  # set height in cms for example\n",
    "    outstanding_debt_amount: sl.Float  # set in dollars for example\n",
    "\n",
    "\n",
    "person = Person()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3628c066-5491-40ff-867a-f3f7dec6f8ae",
   "metadata": {},
   "source": [
    "## Maximum mode\n",
    "\n",
    "To prefer higher numbers. For height, a linear scale is sufficient - which is the default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2df24eaf-f9b8-404b-8b7f-0a9d2c2284df",
   "metadata": {},
   "outputs": [],
   "source": [
    "height_space = sl.NumberSpace(\n",
    "    number=person.height, min_value=100, max_value=200, mode=sl.Mode.MAXIMUM\n",
    ")  # heights out of this range are considered an outlier in our use case"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e47d6bb-4bf0-4fdb-9e7d-6152272c0060",
   "metadata": {},
   "source": [
    "## Minimum mode\n",
    "\n",
    "For preferring lower numbers. As debt often follows a power law distribution (most people have low debt while a small amount of people have extremely high), a logarithmic scale is useful to better embed numbers. Refer to this [discussion](https://www.reddit.com/r/explainlikeimfive/comments/nk990a/eli5_what_is_a_logarithmic_scale_why_is_it_more/) for example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0b19b5b-40eb-47aa-9a62-82348048d165",
   "metadata": {},
   "outputs": [],
   "source": [
    "debt_space = sl.NumberSpace(\n",
    "    number=person.outstanding_debt_amount,\n",
    "    min_value=0,\n",
    "    max_value=1e6,\n",
    "    mode=sl.Mode.MINIMUM,\n",
    "    scale=sl.LogarithmicScale(),\n",
    ")  # debts higher than max_value are effectively capped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0fa1e273-3ab9-4ffe-97d1-327645c31105",
   "metadata": {},
   "outputs": [],
   "source": [
    "person_index = sl.Index([height_space, debt_space])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4aed826a-470e-4e53-8f2a-e8e529373b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "source: sl.InMemorySource = sl.InMemorySource(person)\n",
    "executor = sl.InMemoryExecutor(sources=[source], indices=[person_index])\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "61d76e31-72f4-4404-8cc1-31c08d08d978",
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's insert some numbers tailored to create a point.\n",
    "source.put(\n",
    "    [\n",
    "        {\n",
    "            \"id\": \"person1\",\n",
    "            \"height\": 190,\n",
    "            \"outstanding_debt_amount\": 1e6,\n",
    "        },\n",
    "        {\n",
    "            \"id\": \"person2\",\n",
    "            \"height\": 150,\n",
    "            \"outstanding_debt_amount\": 1e3,  # mediocre in log scale,\n",
    "        },\n",
    "        {\n",
    "            \"id\": \"person3\",\n",
    "            \"height\": 150,\n",
    "            \"outstanding_debt_amount\": 0,\n",
    "        },\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "198f0bce-5d7c-412b-a989-d0ab60982770",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = sl.Query(person_index, weights={height_space: 1, debt_space: 1}).find(person).select_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "aaa75750-3b1c-4d68-af9c-133a249f6b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>outstanding_debt_amount</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>150</td>\n",
       "      <td>0.0</td>\n",
       "      <td>person3</td>\n",
       "      <td>0.603553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>150</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>person2</td>\n",
       "      <td>0.499972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>190</td>\n",
       "      <td>1000000.0</td>\n",
       "      <td>person1</td>\n",
       "      <td>0.349201</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  outstanding_debt_amount       id  similarity_score\n",
       "0     150                      0.0  person3          0.603553\n",
       "1     150                   1000.0  person2          0.499972\n",
       "2     190                1000000.0  person1          0.349201"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = app.query(query)\n",
    "sl.PandasConverter.to_pandas(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97677395-3914-4a9a-a32d-4cbdb05cc885",
   "metadata": {
    "id": "o6dlNq0PMtCQ"
   },
   "source": [
    "The following table explains the results. Notice on the log scale, the mediocre number is much lower than `1e6 / 2` - resulting in more differentiating power for the lower values of the range provided by the embedding."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "753c1a9a-2de7-4763-a598-74b9b5910cea",
   "metadata": {},
   "source": [
    "<style>\n",
    ".table_component {\n",
    "    overflow: auto;\n",
    "    width: 100%;\n",
    "}\n",
    "\n",
    ".table_component table {\n",
    "    border: 1px solid #dededf;\n",
    "    height: 100%;\n",
    "    width: 100%;\n",
    "    table-layout: fixed;\n",
    "    border-collapse: collapse;\n",
    "    border-spacing: 1px;\n",
    "    text-align: left;\n",
    "}\n",
    "\n",
    ".table_component caption {\n",
    "    caption-side: top;\n",
    "    text-align: left;\n",
    "}\n",
    "\n",
    ".table_component th {\n",
    "    border: 1px solid #dededf;\n",
    "    background-color: #eceff1;\n",
    "    color: #000000;\n",
    "    padding: 5px;\n",
    "}\n",
    "\n",
    ".table_component td {\n",
    "    border: 1px solid #dededf;\n",
    "    background-color: #ffffff;\n",
    "    color: #000000;\n",
    "    padding: 5px;\n",
    "}\n",
    "</style>\n",
    "<div class=\"table_component\" role=\"region\" tabindex=\"0\">\n",
    "<table>\n",
    "    <caption>\n",
    "        <p><b>Explanation of results</b></p>\n",
    "    </caption>\n",
    "    <thead>\n",
    "        <tr>\n",
    "            <th><b>Rank</b></th>\n",
    "            <th><b>Id</b></th>\n",
    "            <th><b>Debt status</b></th>\n",
    "            <th><b>Height</b></th>\n",
    "        </tr>\n",
    "    </thead>\n",
    "    <tbody>\n",
    "        <tr>\n",
    "            <td>1<br></td>\n",
    "            <td>person3<br></td>\n",
    "            <td>perfect</td>\n",
    "            <td>mediocre<br></td>\n",
    "        </tr>\n",
    "        <tr>\n",
    "            <td>2</td>\n",
    "            <td>person2</td>\n",
    "            <td>mediocre (log scale)</td>\n",
    "            <td>mediocre</td>\n",
    "        </tr>\n",
    "        <tr>\n",
    "            <td>3</td>\n",
    "            <td>person1</td>\n",
    "            <td>worst possible</td>\n",
    "            <td>almost! perfect</td>\n",
    "        </tr>\n",
    "    </tbody>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "superlinked-py3.11",
   "language": "python",
   "name": "superlinked-py3.11"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
